package org.tea.inventory.admin.web.customer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.tea.inventory.admin.web.admin.BaseController;
import org.tea.inventory.base.contants.UserConstants;
import org.tea.inventory.base.exception.ApplicationException;
import org.tea.inventory.base.response.BaseResponse;
import org.tea.inventory.base.util.ResponseUtil;
import org.tea.inventory.base.util.StringUtils;
import org.tea.inventory.base.util.excel.ExportExcel;
import org.tea.inventory.base.validation.groups.customer.GusetGroup;
import org.tea.inventory.base.validation.groups.customer.GusetGroup;
import org.tea.inventory.domain.tea.CustomerSmall;
import org.tea.inventory.domain.tea.Product;
import org.tea.inventory.domain.tea.WarehouseInventorySerialInfo;
import org.tea.inventory.service.ICustomerGuestService;
import org.tea.inventory.service.ICustomerVipService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 散客 Controller
 *
 * @author dongxb
 * @date 2025-04-16
 */
@Api(tags = "客户管理-散客管理控制器")
@Slf4j
@RestController
@RequestMapping("/admin/customer-guest")
@RequiredArgsConstructor
public class CustomerGuestController extends BaseController
{
    @Autowired
    private ICustomerGuestService customerGuestService;

    /**
     * 查询散客列表
     */
    @GetMapping("/page")
    @ApiOperation(value = "查询散客列表")
    @ApiResponses(
            @ApiResponse(code = 0, message = "成功", response = Product.class)
    )
    public BaseResponse page(CustomerSmall customerSmall)
    {
        startPage();
        List<CustomerSmall> list = customerGuestService.selectCustomerSmallList(customerSmall);
        return getDataTable(list);
    }

    /**
     * 导出散客列表
     */
    @GetMapping("/export")
    @ApiOperation(value = "导出散客列表")
    public void export(CustomerSmall customerSmall, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<>();
        String title = "散客列表";
        String header = "客户编号,客户姓名,性别,联系方式,备注";
        String column = "customerCode,customerName,sexStr,customerPhone,remarks";
        map.put("biaoti", title);
        map.put("header", header);
        map.put("column", column);
        try {
            List<CustomerSmall> list = customerGuestService.selectCustomerSmallList(customerSmall);
            List<Map> listMap = JSONArray.parseArray(JSON.toJSONString(list), Map.class);
            ExportExcel.export(request, response, listMap, map);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取散客详细信息
     */
    @GetMapping("/info")
    @ApiOperation(value = "获取散客详细信息")
    public BaseResponse getInfo(Long id)
    {
        return ResponseUtil.ok(customerGuestService.selectCustomerSmallById(id));
    }

    /**
     * 新增散客
     */
    @PostMapping(value = "/add")
    @ApiOperation(value = "新增散客")
    public BaseResponse add(@Validated({GusetGroup.class}) @RequestBody CustomerSmall customerSmall)
    {
        if (!checkCodeOrNameUnique(customerSmall)) throw new ApplicationException(-1, "新增散客失败：会员编号或会员名称已经存在。");
        return ResponseUtil.ok(customerGuestService.insertCustomerSmall(customerSmall));
    }

    /**
     * 修改散客
     */
    @PostMapping(value = "/edit")
    @ApiOperation(value = "修改散客")
    public BaseResponse edit(@Validated({GusetGroup.class}) @RequestBody CustomerSmall customerSmall)
    {
        if (!checkCodeOrNameUnique(customerSmall)) throw new ApplicationException(-1, "修改散客失败：会员编号或会员名称已经存在。");
        return ResponseUtil.ok(customerGuestService.updateCustomerSmall(customerSmall));
    }

    public boolean checkCodeOrNameUnique(CustomerSmall customer) {
        List<CustomerSmall> list = customerGuestService.selectCustomerSmallByCodeOrName(customer);
        if (StringUtils.isNull(list) || list.size() == 0) {
            return UserConstants.UNIQUE;
        }
        return UserConstants.NOT_UNIQUE;
    }

    /**
     * 更新企业客户状态
     *
     * @param id 企业客户主键
     * @param status 企业客户更新前状态
     * @return 结果
     */
    /*@PostMapping(value = "/updateStatus")
    @ApiOperation(value = "更新企业客户状态")
    public BaseResponse updateStatus(@ApiParam(value = "更新id") Long id , @ApiParam(value = "更新前状态") Integer status)
    {
        return ResponseUtil.ok(customerGuestService.updateStatus(id , status));
    }*/

    /**
     * 删除散客
     */
    @PostMapping(value = "/remove")
    @ApiOperation(value = "删除散客")
    public BaseResponse remove(@ApiParam(value = "删除散客id集合") Long id)
    {
        return ResponseUtil.ok(customerGuestService.deleteCustomerSmallById(id));
    }
}
